
#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"
u8 ILI7835_Gamma_R[48]=
{
	0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,
	0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
};
u8 ILI7835_Gamma_G[48]=
{
	0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
	0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f
};
u8 ILI7835_Gamma_B[48]=
{
	0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,
	0x7e,0x7f,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f
};

u8 ILI7835_otptime=0;
u8 ILI7835_Gamma_Gray[24] =
{	
	255, 247, 225, 198, 177, 159, 134, 114, 104, 93, 81, 65, 51, 41, 35, 29, 22, 17, 13, 9, 5, 3, 1,  0 
};

void ILI7835_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{ 
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);	
	SSD2828_W_Array(SigMode,channel,buffer,2);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	delay_ms(5);
	buffer[4] = Uart_Error_None;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}

void ILI7835_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
    
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
	
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);    
	buffer[1]=0x02;  //0x22,0x02 共用串口返回数据处理部分
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}
void ILI7835_HSRead_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
    //bit7 Read Enable:1 – Read operation; bit4 1:HS CLOCK Disable
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);	//SSD2828_W_Reg(SigMode,channel,0xB7,HS_DCS_Short_Read);
	delay_ms(1);
    
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
	
//    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);    
	
	buffer[1]=2;  //0x22,0x02 共用串口返回数据处理部分
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);  
}

void ILI7835_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//	u8 buffer0[5];//切换Page_0_R51
	u8 buffer2[5];//切换Page_0_R53
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	//SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
	////SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(5);
	
	if((buffer[3]&0x07) == 0x00)		//Normal_Gamma 1
    {
//        buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x35;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
//		buffer0[0] = 0x03;
//		buffer0[1] = 0x51;
//		buffer0[2] = 0x03;
//		buffer0[3] = 0xff;
//		SSD2828_W_Array(SigMode,channel,buffer0,0);
//		delay_ms(5);			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA1  page9
		buffer1[3] = 0x35;
		buffer1[4] = 0x09;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
	else if((buffer[3]&0x07) == 0x01)	//Normal_Gamma_2
	{
//	    buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x35;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
//		buffer0[0] = 0x03;
//		buffer0[1] = 0x51;
//		buffer0[2] = 0x01;
//		buffer0[3] = 0x19;
//		SSD2828_W_Array(SigMode,channel,buffer0,0);
//		delay_ms(5);			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA2  pageA
		buffer1[3] = 0x35;
		buffer1[4] = 0x0A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x02)	//Normal_Gamma_3
	{
//	    buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x35;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
//		buffer0[0] = 0x03;
//		buffer0[1] = 0x51;
//		buffer0[2] = 0x01;
//		buffer0[3] = 0x19;
//		SSD2828_W_Array(SigMode,channel,buffer0,0);
//		delay_ms(5);			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA3  pageB
		buffer1[3] = 0x35;
		buffer1[4] = 0x0B;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}	
	else if((buffer[3]&0x07) == 0x03)	//Normal_Gamma_4
	{
//	    buffer1[0] = 0x04;
//        buffer1[1] = 0xFF;
//        buffer1[2] = 0x78;                //   page0
//		buffer1[3] = 0x35;
//		buffer1[4] = 0x00;
//		SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);
//		buffer0[0] = 0x03;
//		buffer0[1] = 0x51;
//		buffer0[2] = 0x01;
//		buffer0[3] = 0x19;
//		SSD2828_W_Array(SigMode,channel,buffer0,0);
//		delay_ms(5);			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA4  pageC
		buffer1[3] = 0x35;
		buffer1[4] = 0x0C;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x04)	//HBM_Gamma
	{
	    buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x35;
		buffer1[4] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
//		buffer0[0] = 0x03;
//		buffer0[1] = 0x51;
//		buffer0[2] = 0x03;
//		buffer0[3] = 0xFF;
//		SSD2828_W_Array(SigMode,channel,buffer0,0);
//		delay_ms(5);
		buffer2[0] = 0x02;
		buffer2[1] = 0x53;
		buffer2[2] = 0xEC;
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(5);
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // HBM_GAMMA2  pageE
		buffer1[3] = 0x35;
		buffer1[4] = 0x0E;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x05)	//AOD_Gamma
	{
	    buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                //   page0
		buffer1[3] = 0x35;
		buffer1[4] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
//		buffer0[0] = 0x03;
//		buffer0[1] = 0x51;
//		buffer0[2] = 0x03;
//		buffer0[3] = 0xFF;
//		SSD2828_W_Array(SigMode,channel,buffer0,0);
//		delay_ms(5);
		buffer2[0] = 0x02;
		buffer2[1] = 0x53;
		buffer2[2] = 0x2E;
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(5);
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // AOD_GAMMA  pageF
		buffer1[3] = 0x35;
		buffer1[4] = 0x0F;
        SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
		
	for( i = 0;i<buffer[2]-1;i++) 
    {	       
		buffer1[0] = 0x02;
		SSD2828_W_Reg(SigMode ,channel,0xBC, 2);//数据长度
        if((buffer[3]&0x70)==0x10)	
		{
//			buffer1[1] = ILI7835_Gamma_R[i];
//			buffer1[2] = buffer[i+4];
//			SSD2828_W_Array(SigMode,channel,buffer1,0);
//			delay_ms(5);
			
			SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8|ILI7835_Gamma_R[i])); 
			
		}
		 
        else if((buffer[3]&0x70)==0x20)
        {
//			buffer1[1] = ILI7835_Gamma_G[i];
//			buffer1[2] = buffer[i+4];
//			SSD2828_W_Array(SigMode,channel,buffer1,0);
//			delay_ms(5);
			SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|ILI7835_Gamma_G[i]);
			
		} 
		 
        else if((buffer[3]&0x70)==0x30)
        {
//			buffer1[1] = ILI7835_Gamma_B[i];
//			buffer1[2] = buffer[i+4];
//			SSD2828_W_Array(SigMode,channel,buffer1,0);
//			delay_ms(5);
			SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|ILI7835_Gamma_B[i]);
		}
		 																		
        delay_ms(5);
    }
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);//低速下写完，切换回高速
		buffer[4] = Uart_Error_None;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 
}
void ILI7835_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	
//    u8 buffer0[5];//切换Page_0_R51
//	u8 buffer2[5];//切换Page_0_R53
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);//7835目前，低速不能超过10M/s,高速不能1.2G/b
	delay_ms(15);
	if((buffer[3]&0x07) == 0x00)		//Normal_Gamma 1
    {						
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA1  page9
		buffer1[3] = 0x35;
		buffer1[4] = 0x09;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
	else if((buffer[3]&0x07) == 0x01)	//Normal_Gamma_2
	{		
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA2  pageA
		buffer1[3] = 0x35;
		buffer1[4] = 0x0A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x02)	//Normal_Gamma_3
	{			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA3  pageB
		buffer1[3] = 0x35;
		buffer1[4] = 0x0B;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}	
	else if((buffer[3]&0x0f) == 0x03)	//Normal_Gamma_4
	{			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA4  pageC
		buffer1[3] = 0x35;
		buffer1[4] = 0x0C;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x04)	//HBM_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // HBM_GAMMA2  pageE
		buffer1[3] = 0x35;
		buffer1[4] = 0x0E;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x05)	//AOD_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // AOD_GAMMA2  pageF
		buffer1[3] = 0x35;
		buffer1[4] = 0x0F;
        SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
	delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
    for( i = 0;i<buffer[2]-1;i++)			 	
    {  	
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
        buffer1[0] = 0x01;
		if((buffer[3]&0x70)==0x10)		//0x10 Red_Register
            buffer1[1]=ILI7835_Gamma_R[i];  
        else if((buffer[3]&0x70)==0x20)	//0x20 Blue_Register
            buffer1[1]=ILI7835_Gamma_G[i]; 
        else if((buffer[3]&0x70)==0x30)	//0x30 Green_Register
            buffer1[1]=ILI7835_Gamma_B[i]; 																		
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_us(200);
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
        delay_us(200);
    }	
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ??? Gamma??  :ok 
	
}




void ILI7835_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u8 OTP_Code[12]={0x4D,0x04,0x4E,0x01,0x53,0x55,0x54,0xAA,0x55,0x66,0x56,0xCC};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);//7835目前，低速不能超过10M/s,高速不能1.2G/b
	delay_ms(15);
	
	{
		buffer1[0] = 0x04;
		buffer1[1] = 0xFF;
		buffer1[2] = 0x78;                
		buffer1[3] = 0x35;
		buffer1[4] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	{
		buffer1[0] = 0x01;
		buffer1[1] = 0x28;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	
	{
		buffer1[0] = 0x04;
		buffer1[1] = 0xFF;
		buffer1[2] = 0x78;                
		buffer1[3] = 0x35;
		buffer1[4] = 0x06;
		SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	for(i=0;i<sizeof(OTP_Code);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = OTP_Code[i];
		buffer1[2] = OTP_Code[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(3);
		i+=1;
	}
	delay_ms(2500);
	SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package 		 	                          
	do
	{                        
			buffer1[0] = 0x01;
			buffer1[1] = 0x4C;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(6);
			buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);   
			delay_ms(100);
	}while((buffer[4]&0x01)!=0x00);
	
	buffer[0] = 0x56;
	buffer[1] = 0x07;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 		
}
void ILI7835_AutoGamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
//	u8 buffer1[3];
	//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);	
	delay_ms(5);	
	
	
	SSD2828_W_Reg(SigMode,channel,0xBC,0x02);	
    for( i = 0;i<buffer[2];i++) 
    {	
//		buffer1[0] = 0x02;
//      buffer1[1] = buffer[i+3];
//      buffer1[2] = buffer[i+4];
//      SSD2828_W_Array(SigMode,channel,buffer1,0);		
        SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|buffer[i+3]); 
        i++;
        delay_ms(2);
    }				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
void ILI7835_HSRead_Register_All(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//u8 buffer0[5];//切换Page_0_R51
	//u8 buffer2[5];//切换Page_0_R53
	u8 buffer1[5];
//	//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
//	//delay_ms(15);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	delay_ms(5);
	
	if((buffer[3]&0x07) == 0x00)		//Normal_Gamma 1
    {						
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA1  page9
		buffer1[3] = 0x35;
		buffer1[4] = 0x09;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
	else if((buffer[3]&0x07) == 0x01)	//Normal_Gamma_2
	{		
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA2  pageA
		buffer1[3] = 0x35;
		buffer1[4] = 0x0A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x07) == 0x02)	//Normal_Gamma_3
	{			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA3  pageB
		buffer1[3] = 0x35;
		buffer1[4] = 0x0B;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}	
	else if((buffer[3]&0x0f) == 0x03)	//Normal_Gamma_4
	{			
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // Normal_GAMMA4  pageC
		buffer1[3] = 0x35;
		buffer1[4] = 0x0C;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x04)	//HBM_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // HBM_GAMMA2  pageE
		buffer1[3] = 0x35;
		buffer1[4] = 0x0E;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
	}
	else if((buffer[3]&0x0f) == 0x05)	//AOD_Gamma
	{
		buffer1[0] = 0x04;
        buffer1[1] = 0xFF;
        buffer1[2] = 0x78;                // AOD_GAMMA2  pageF
		buffer1[3] = 0x35;
		buffer1[4] = 0x0F;
        SSD2828_W_Array(SigMode,channel,buffer1,0);		
	} 
	delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); //LP_B7_Data|0x0080
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);//返回两个字节；
	for( i = 0;i<(buffer[2]-1)/3;i++) 
    {	  
		
		buffer1[0] = 0x01;
        buffer1[1] = ILI7835_Gamma_R[i];
        SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(3);
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
		delay_ms(3);
		
		buffer1[0] = 0x01;
        buffer1[1] = ILI7835_Gamma_G[i];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(3);
        buffer[4+i+48]=SSD2828_R_Reg(SigMode,channel,0xFF);
		delay_ms(3);
		
		buffer1[0] = 0x01;
        buffer1[1] = ILI7835_Gamma_B[i];
        SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(3);
		buffer[4+i+96]=SSD2828_R_Reg(SigMode,channel,0xFF);
		delay_ms(3);
    }
    
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);


}
//---------------------------------------------------------------------------------------------------------------
void ILI7835_HSRead_Register_Sample(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	           
}


//进入处理
void ProcessForIc56( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
		
		case 0x01:                                      //写寄存器
				ILI7835_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x02:                                      //读寄存器		
				ILI7835_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;
		case 0x23://读取所有寄存器值 		
				ILI7835_HSRead_Register_All(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;
		case 0x33://读取所有寄存器值 		
//				ILI7835_HSWrite_Register_All(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;		
		case 0x07:                                      //Gamma OTP
				ILI7835_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
				break;
		case 0x08:   									//写Gamma				
				ILI7835_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x09:                                      //读 Gamma
				ILI7835_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
				break;
		case 0xDD:                                      // AutoGamma
				ILI7835_AutoGamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
				break;        
		default:break;
	}			
}


/*
#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"
u8 ILI7835_Gamma_R[48]=
{
	0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,
	0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
};
u8 ILI7835_Gamma_G[48]=
{
	0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
	0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f
};
u8 ILI7835_Gamma_B[48]=
{
	0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,
	0x7e,0x7f,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f
};

u8 ILI7835_otptime=0;
u8 ILI7835_Gamma_Gray[24] =
{	
	255, 247, 225, 198, 177, 159, 134, 114, 104, 93, 81, 65, 51, 41, 35, 29, 22, 17, 13, 9, 5, 3, 1,  0 
};

void ILI7835_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//SSD2828_W_Reg(SigMode,channel,0xB7,(LP_B7_Data&0xFBFF)|0x0400); 
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);	
	SSD2828_W_Array(SigMode,channel,buffer,2);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	delay_ms(5);
	buffer[4] = Uart_Error_None;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}

void ILI7835_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	delay_ms(5);
    
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
	
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);    
	buffer[1]=2;  //0x22,0x02 共用串口返回数据处理部分
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}
void ILI7835_HSRead_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
    //bit7 Read Enable:1 – Read operation; bit4 1:HS CLOCK Disable
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data|0x0080);	//SSD2828_W_Reg(SigMode,channel,0xB7,HS_DCS_Short_Read);
	delay_ms(1);
    
	Mipi_Read(pdev,SigMode,channel,buffer[3],buffer[2]-1,Buffer_ReadCode);
	memcpy(buffer+4,Buffer_ReadCode,buffer[2]-1);
	
//    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);    
	
	buffer[1]=2;  //0x22,0x02 共用串口返回数据处理部分
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);  
}
void ILI7835_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
        
    for( i = 0;i<(buffer[2]-1)/3;i++) 
    {	  
		if(i==47)
		{delay_ms(5);}
		if(i==46)
		{delay_ms(5);}
		buffer1[0] = 0x02;
        buffer1[1] = ILI7835_Gamma_R[i];
        buffer1[2] = buffer[i+4];
        SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer1[0] = 0x02;
        buffer1[1] = ILI7835_Gamma_G[i];
        buffer1[2] = buffer[i+4+48];
        SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		buffer1[0] = 0x02;
        buffer1[1] = ILI7835_Gamma_B[i];
        buffer1[2] = buffer[i+4+96];
        SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
    }
    
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
	
}
void ILI7835_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	delay_ms(15);
	
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
    for( i = 0;i<(buffer[2]-1)/3;i++)			 	
    {  	
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
        buffer1[0] = 0x01;
        buffer1[1] =  ILI7835_Gamma_R[i];
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);

        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
        buffer1[0] = 0x01;
        buffer1[1] =  ILI7835_Gamma_G[i];
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
        buffer[4+i+48]=SSD2828_R_Reg(SigMode,channel,0xFF);
		
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
        buffer1[0] = 0x01;
        buffer1[1] =  ILI7835_Gamma_B[i];
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
        buffer[4+i+96]=SSD2828_R_Reg(SigMode,channel,0xFF);
		
    }	
   
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);
	
}
//void ILI7835_Gamma_Mapping(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
//{
//	u8 buffer1[5];
//	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
//	delay_ms(5);
//	buffer1[0] = 0x02;
//	buffer1[1] = 0xfe;
//	buffer1[2] = 0x70;
//	SSD2828_W_Array(SigMode,channel,buffer1,0);
//	
//	buffer1[1] = 0x48;
//	buffer1[2] = buffer[3];
//	SSD2828_W_Array(SigMode,channel,buffer1,0);		
//	
//	for( i = 1;i<buffer[2];i++) 
//	{	 
//		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+3]<<8)|ILI7835_Gamma_Points[i]);   																	
//		delay_ms(1);
//	}
//	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
//	buffer[4] = Uart_Error_None;
//	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ?Gamma??  :ok  		

//}



void ILI7835_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u8 R_30,R_31,R_32,R_33,R_34,R_35,R_36,R_E6,R_E7;
	u8 ILI7835_OTP[22]={0xFE,0x42,0x03,0x10,0x04,0x32,0x05,0x10,0x06,0x32,0x07,0x10,0x08,0x32,0x09,0x10,0x0A,0x32,0x0B,0x10,0x0C,0x32};
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0xfffd);
	
	delay_ms(5);
	
	if(buffer[4]==6) //RCN
	{
		buffer1[0] = 0x02;
		buffer1[1] = 0xFE;
		buffer1[2] = 0xD0;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  				
		buffer1[0] = 0x02;
		buffer1[1] = 0x99;
		buffer1[2] = 0x20;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  	
		buffer1[0] = 0x02;
		buffer1[1] = 0xFE;
		buffer1[2] = 0x40;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  		
		buffer1[0] = 0x02;
		buffer1[1] = 0x9A;
		buffer1[2] = 0x00;
		SSD2828_W_Array(SigMode,channel,buffer1,0);  								
	}
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xFE;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  		
	
	buffer1[0] = 0x01;
	buffer1[1] = 0x28;
	SSD2828_W_Array(SigMode,channel,buffer1,0); 	
	delay_ms(50);
	buffer1[0] = 0x01;
	buffer1[1] = 0x10;
	SSD2828_W_Array(SigMode,channel,buffer1,0); 	
	delay_ms(50);	
	if(buffer[4]==1 || buffer[4]==2|| buffer[4]==3||buffer[4]==4)
	{
	for(i=0;i<22;i++)//ILI7835新增
	{
		buffer1[0]=0x02;
		buffer1[1]=ILI7835_OTP[i];
		buffer1[2]=ILI7835_OTP[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(5);
		i++;
	}
	
	buffer1[0] = 0x02;	
	buffer1[1] = 0xFE;
	buffer1[2] = 0x40;
	SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
	delay_ms(50);
	buffer1[0] = 0x02;
	buffer1[1] = 0xF2;
	buffer1[2] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0			
	delay_ms(100);
	if(buffer[4]==1)//GAM1-50H
	{
		buffer1[0] = 0x02;
		if(buffer[5]==1) //gamma1
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x01;
		}
		else if(buffer[5]==5) //gamma5
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x10;		
		}
		else if(buffer[5]==9) //gamma9
		{
			buffer1[1] = 0xED;
			buffer1[2] = 0x01;		
		}
		else if(buffer[5]==13) //gamma13
		{
			buffer1[1] = 0xED;
			buffer1[2] = 0x10;	
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);					
	}
	//---------------------------------------------------		
	else if(buffer[4]==2) //52
	{
		buffer1[0] = 0x02;
		if(buffer[5]==2) //gamma2
		{							
			buffer1[1] = 0xEE;
			buffer1[2] = 0x02;
		}
		else if(buffer[5]==6) //gamma6
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x20;	
		}
		else if(buffer[5]==10)//gamma10
		{
			buffer1[1] = 0xED;
			buffer1[2] = 0x02;			
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
	else if(buffer[4]==3)			//GAM1-54H	
	{
		buffer1[0] = 0x02;
		if(buffer[5]==3) //gamma3
		{				
			buffer1[1] = 0xEE;
			buffer1[2] = 0x04;   
		}
		else if(buffer[5]==7) //gamma7
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x40;   		
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
	}
	else if(buffer[4]==4)			//GAM1-56H	
	{
		buffer1[0] = 0x02;
		if(buffer[5]==4) //gamma4
		{	
			buffer1[1] = 0xEE;
			buffer1[2] = 0x08;
		}
		else if(buffer[5]==8) //gamma8
		{
			buffer1[1] = 0xEE;
			buffer1[2] = 0x80;		
		}
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
	}
}
	
	else if(buffer[4]==5)		//PAGE-A0/D0/40/60/70/90/72/74/F2
	{
		buffer1[0] = 0x02;			
		if(buffer[3]==1)
		{
			buffer1[0] = 0x02;	//切fe,D0
			buffer1[1] = 0xFE;
			buffer1[2] = 0xD0;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xE5;
			buffer1[2] = 0x03; //P-D0/A0
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			buffer1[1] = 0xF6;
			buffer1[2] = 0x17;  //P-40/60/70/90
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xF8;
			buffer1[2] = 0x20; //P-72
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			buffer1[1] = 0xEC;
			buffer1[2] = 0x08; //P-74
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xF0;
			buffer1[2] = 0x04; //P-F2
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		else if(buffer[3]==2)
		{
			buffer1[0] = 0x02;	//切fe,D0
			buffer1[1] = 0xFE;
			buffer1[2] = 0xD0;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xE4;
			buffer1[2] = 0x02;//P-D0
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			buffer1[1] = 0xF7;
			buffer1[2] = 0x17;  //P-40/60/70/90
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
			buffer1[1] = 0xF9;
			buffer1[2] = 0x20; //P-72
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			buffer1[1] = 0xEC;
			buffer1[2] = 0x10; //P-74
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			buffer1[1] = 0xF0;
			buffer1[2] = 0x08; //P-F2
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			
		}
	}
	else if(buffer[4]==6)		//20\22\26
	{
		if(buffer[3]==1)
		{	
			buffer1[1] = 0xF8;
			buffer1[2] = 0x16;
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}
		else if(buffer[3]==2)
		{
			buffer1[1] = 0xF9;
			buffer1[2] = 0x16;
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
		}
	}
	else if(buffer[4]==7) //RCN
	{
		if(buffer[3]==1)
		{				
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0);				
			buffer1[1] = 0xF8;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
		}
		else if(buffer[3]==2)
		{
			buffer1[1] = 0xFE;
			buffer1[2] = 0x40;
			SSD2828_W_Array(SigMode,channel,buffer1,0);				
			buffer1[1] = 0xF8;
			buffer1[2] = 0x80;
			SSD2828_W_Array(SigMode,channel,buffer1,0);			
		}
	}
	//----------------
	buffer1[0] = 0x02;
	buffer1[1] = 0xFE;
	buffer1[2] = 0x40;
	SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
	delay_ms(50);
	buffer1[0] = 0x02;//MTPCTR
	buffer1[1] = 0xF2;
	buffer1[2] = 0x01;
	SSD2828_W_Array(SigMode,channel,buffer1,0);    
	buffer1[0] = 0x02;
	buffer1[1] = 0xF3;
	buffer1[2] = 0xA5;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  
	buffer1[0] = 0x02;
	buffer1[1] = 0xF4;
	buffer1[2] = 0x5A;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  
	buffer1[0] = 0x02;
	buffer1[1] = 0xF5;
	buffer1[2] = 0x3C;
	SSD2828_W_Array(SigMode,channel,buffer1,0);  		
	delay_ms(1500);         // wait for >400 ms   
	buffer[4] = Uart_Error_None;
	//buffer[3] = Uart_Error_None;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ?OTP??  :ok		
}
void ILI7835_AutoGamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
  	u8 buffer1[3];

	//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);

	if(buffer[15]!=0x80)  //采用默认PAGE的标志位:如果buffer[15]=0x80,则不强切进入0x50
	{
		buffer1[0] = 0x02;	
		
		buffer1[1] = 0xFE;
		buffer1[2] = 0x70;                
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[1] = 0x71;
		buffer1[2] = 0x00;                
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[1] = 0x72;
		buffer1[2] = 0x31;                // 强切GAMMA PAGE
		SSD2828_W_Array(SigMode,channel,buffer1,0);		

		buffer1[1] = 0xFE;
		buffer1[2] = 0x50;                
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
	}	

		
	SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
    for( i = 0;i<buffer[2]-1;i++) 
    {	 
			SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|buffer[i+3]);  
			i++;
			delay_ms(2);
    }				
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
/////----------------------------------------------RM692AX----------------------------------------------
void ILI7835_Write51_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
void ILI7835_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 buffer1[3];
    u16 ED_D,EE_D,EB_D,EC_D,E9_D,E8_D,E7_D,E6_D;
    u8 RM692AX_otptime = 0;
    if(SigMode != CMD_Mode)
	  delay_ms(5);
		buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x40;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // Page 0  ?69300???
    buffer1[0] = 0x02;
    buffer1[1] = 0xE5;
    buffer1[2] = 0x02;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // otp_load_en   ?69300?? 														
    buffer1[0] = 0x02;
    buffer1[1] = 0xF2;
    buffer1[2] = 0x80;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // Margin Read Check   ?69300??
		delay_ms(50);
		buffer1[0] = 0x02;
		buffer1[1] = 0xfe;
		buffer1[2] = 0x44;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
    delay_ms(100);
    buffer1[0] = 0x01;
    buffer1[1] = 0x36;         //?69300???
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                                  
    EE_D=SSD2828_R_Reg(SigMode,channel,0xFF); 
    delay_ms(100);		
    buffer1[0] = 0x01;
    buffer1[1] = 0x35;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                             
    ED_D=SSD2828_R_Reg(SigMode,channel,0xFF);
		delay_ms(100);
	  buffer1[0] = 0x01;
    buffer1[1] = 0x34;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                             
    EC_D=SSD2828_R_Reg(SigMode,channel,0xFF);

	buffer1[0] = 0x01;
    buffer1[1] = 0xE7;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                             
    E7_D=SSD2828_R_Reg(SigMode,channel,0xFF);
	
	buffer1[0] = 0x01;
    buffer1[1] = 0xE6;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                             
    E6_D=SSD2828_R_Reg(SigMode,channel,0xFF);
	
	if(buffer[3] == 0x00||buffer[3] == 0x01||buffer[3] == 0x02||buffer[3] == 0x03||buffer[3]==0x04)
	{
		if(buffer[3] == 0x00)       //??gamma1??
    {
        if(EE_D&0X01)
            RM692AX_otptime = 1;
        if(ED_D&0X01)
            RM692AX_otptime = 2;
				if(EC_D&0X01)
            RM692AX_otptime = 3;
    }
    else if(buffer[3] == 0x01)  //??gamma2??
    {
        if(EE_D&0X02)
            RM692AX_otptime = 1; 
        if(ED_D&0X02)
            RM692AX_otptime = 2;   
		    if(EC_D&0X02)
            RM692AX_otptime = 3; 			
    }
    else if(buffer[3] == 0x02)  //??gamma3??
    {
        if(EE_D&0X04)
            RM692AX_otptime = 1;
        if(ED_D&0X04)
            RM692AX_otptime = 2; 
				if(EC_D&0X04)
            RM692AX_otptime = 3; 				
    }
    else if(buffer[3] == 0x03)  //??Gamma4??
    {
        if(EE_D&0X08)
            RM692AX_otptime = 1;
        if(ED_D&0X08)
            RM692AX_otptime = 2;  
        if(EC_D&0X08)
            RM692AX_otptime = 3;        
    }
	else if(buffer[3] ==0x04)
	{
		if(E7_D&0X03)
            RM692AX_otptime = 1;
        if(E6_D&0X03)
            RM692AX_otptime = 2;
	}
	}
    buffer[4]=RM692AX_otptime;
    RM692AX_otptime=0x00;
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[3] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           
}
void ILI7835_Change_Gray(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
	delay_ms(5);
	buffer1[0] = 0x02;
	buffer1[1] = 0xfe;
	buffer1[2] = 0x70;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	for( i = 0;i<buffer[2];i++) 
	{	 
		SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+3]<<8)|ILI7835_Gamma_Gray[i]);   																	
		delay_ms(3);
	}
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	buffer[4] = Uart_Error_None;
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //
}
//---------------------------------------------------------------------------------------------------------------
void ILI7835_Read_MTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];	

	u8 Addr[]={0xB4,0xB3,0xB2,0xB7,0xB6,0xB5};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);    
			buffer1[0] = 0x02;
			buffer1[1] = 0xfe;
			buffer1[2] = 0x44;
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(20);		
	  for(i=0;i<6;i++)
		{
			SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:??? bit4 1:HS CLOCK Disable
			delay_ms(5);					
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
			buffer1[0] = 0x01;
			buffer1[1] = Addr[i];
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
			delay_ms(20);
			buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
		}
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
		
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);
}

//---20190713-----ILI7835_Read_All_MTP_Reg
void ILI7835_Read_All_MTP_Reg(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 SendBuff[3];	
	u8 Addr[21]={0xe7,0xe6,0xAE ,0xAF ,0xB0 ,0xB1 ,0xB4 ,0xB3 ,0xB2 ,0xA9 ,0xAA,0xAB,0xBA,0xBB,0xAD ,0xAC ,0xB7 ,0xB6,0xB5,0xB9 ,0xB8};
	//---READ 0xD0	
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);
	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);    
	SendBuff[0] = 0x02;
	SendBuff[1] = 0xfe;
	SendBuff[2] = 0xD0;
	SSD2828_W_Array(SigMode,channel,SendBuff,0); 
	delay_ms(20);		
	for(i=0;i<2;i++)
	{
		SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:??? bit4 1:HS CLOCK Disable
		delay_ms(5);					
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
		SendBuff[0] = 0x01;
		SendBuff[1] = Addr[i];
		SSD2828_W_Array(SigMode,channel,SendBuff,0); 
		delay_ms(20);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
	}
	//---READ 0x44
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);		
	delay_ms(5);
	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);    
	SendBuff[0] = 0x02;
	SendBuff[1] = 0xfe;
	SendBuff[2] = 0x44;
	SSD2828_W_Array(SigMode,channel,SendBuff,0); 
	delay_ms(20);		
	for(i=2;i<21;i++)
	{
		SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);  //bit7 1:??? bit4 1:HS CLOCK Disable
		delay_ms(5);					
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
		SendBuff[0] = 0x01;
		SendBuff[1] = Addr[i];
		SSD2828_W_Array(SigMode,channel,SendBuff,0); 
		delay_ms(20);
		buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);
	}
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
		
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);
}



void ProcessForIc56( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
		case 0x51:
		case 0x01:                                      //写寄存器
				ILI7835_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x02:                                      //读寄存器		
				ILI7835_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;
		case 0x22: 		
				ILI7835_HSRead_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);				
				break;		
//		case 0x03:                                      //Gamma OTP
//				ILI7835_Gamma_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
//				break;
		case 0x04:   									//写Gamma				
				ILI7835_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x05:                                      //读 Gamma
				ILI7835_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);    break;
//		case 0x06://otp time read
//				ILI7835_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
//		case 0x07://otp time
//                {
//                    ILI7835_otptime=USB_Rx_Buffer[4];
//                    USB_Rx_Buffer[4] = Uart_Error_None;
//                    STM2PC_RM671xx(pdev,CDC_IN_EP,USB_Rx_Buffer,USB_Rx_Buffer[2]+3);  
//                }break;		
//		case 0x08:    // auto Gamma 
//				ILI7835_AutoGamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
//				break;	
//		case 0x09:
//				ILI7835_Change_Gray(pdev,CDC_IN_EP,OLED.SigMode ,1,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
////		case 0x0A: 
////				ILI7835_Gamma_Mapping(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
////				break;
//		case 0x0B:
//				ILI7835_Read_MTP_Time(pdev,ep_addr,SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data);
//				break;
//        case 0x1B:
//				ILI7835_Read_All_MTP_Reg(pdev,ep_addr,SigMode ,channel,buffer,LP_B7_Data,HS_B7_Data);
//				break;
        
        
		default:break;
	}			
}


*/















